package com.szqhyxj.youxiangju.biz.card.credit;

import com.szqhyxj.youxiangju.biz.card.CardModel;
import com.szqhyxj.youxiangju.sys.BaseResult;
import com.szqhyxj.youxiangju.sys.AbBasePresenter;
import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * XiaoPanYu
 * on 2018/10/31
 */
public class CreaditDetailsPresenter extends AbBasePresenter implements  CreaditDetailsContarct.Presenter{



    private  CreaditDetailsContarct.View view;
    private List<Disposable> disposables = new ArrayList<>();
    private CardModel.CreditEntity creditEntity;



    public CreaditDetailsPresenter( CreaditDetailsContarct.View view){
        this.view = view ;
        creditEntity = new CardModel.CreditEntity();

    }

    @Override
    public void init(String cardNo){
        disposables.add(CardModel.getcreditcard(cardNo)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Consumer<BaseResult<CardModel.Card>>(){
                    @Override
                    public void accept(BaseResult<CardModel.Card> cardBaseResult) throws Exception{
                        view.dismissLoading();
                        if(cardBaseResult.businessLogicSuccess()){
                            view.showCreaditDetails(cardBaseResult.getData());
                        }else {
                            view.showToast(cardBaseResult.getMessage());
                        }
                    }
                }, new Consumer<Throwable>(){
                    @Override
                    public void accept(Throwable throwable) throws Exception{
                        view.showToast(throwable.getMessage());
                        view.dismissLoading();
                    }
                }));

    }

    @Override
    public void setBankCardNo(String st){
        creditEntity.setBankCardNo(st);
    }

    @Override
    public void setFixedAmount(String st){
        creditEntity.setFixedAmount(st);
    }

    @Override
    public void setBillingDate(String st){
        creditEntity.setBillingDate(st);
    }

    @Override
    public void setDueDate(String st){
        creditEntity.setDueDate(st);
    }

    @Override
    public void save(){

        disposables.add(
                CardModel
                        .bankCreditCard(creditEntity)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Consumer<BaseResult>(){
                            @Override
                            public void accept(BaseResult baseResult) throws Exception{
                                view.dismissLoading();
                                if(baseResult.businessLogicSuccess()){
                                    view.finishThis();
                                    view.showToast("修改成功");
                                }else {
                                    view.showToast(baseResult.getMessage());

                                }
                            }
                        }, new Consumer<Throwable>(){
                            @Override
                            public void accept(Throwable throwable) throws Exception{
                                view.showToast(throwable.getMessage());
                                view.dismissLoading();
                            }
                        }));
    }

    @Override
    public void relieveBind(String cardNo){
        disposables.add(CardModel.deleteCard(cardNo)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<BaseResult>(){
                    @Override
                    public void accept(BaseResult baseResult) throws Exception{
                        view.dismissLoading();
                        if(baseResult.businessLogicSuccess()){
                            view.navRelieveCreaditOk();
                        }else {
                           view.showToast(baseResult.getMessage());
                        }
                    }
                }, new Consumer<Throwable>(){
                    @Override
                    public void accept(Throwable throwable) throws Exception{
                        view.showToast(throwable.getMessage());
                        view.dismissLoading();
                    }
                }));

    }

    ;

    @Override
    public void onDestroy(){
        for (Disposable disposable : disposables) {
            if (disposable != null) {
                disposable.dispose();
            }
        }
    }

    @Override
    public void addDisposables(Disposable disposable){
        super.addDisposables(disposable);
        view.showLoading();
    }
}
